/*eeeeeeeeeeeeee
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Teste.java
 *
 * Created on 08/10/2009, 19:50:41
 */

package Util;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author Administrador
 */
public class Teste extends javax.swing.JFrame {

    /** Creates new form Teste */
    public Teste() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jScrollPane1.setViewportView(jTree1);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 796, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(266, 266, 266)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 391, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(175, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        try{
        JOptionPane.showMessageDialog(null,"Entrou evento");
        java.sql.PreparedStatement stm;
        ResultSet rs=null;
        ResultSet rs1=null;
                if(!FabricaConexoes.verificaConexao()) FabricaConexoes.getConexao();
                stm = FabricaConexoes.returnStatement("Select * from Processo_Atividade where raiz = 0 and idproc = 1 and idfin = 0");
                rs = FabricaConexoes.returnResult(stm);
                stm = FabricaConexoes.returnStatement("Select * from Processo where idprocesso = 1");
                rs1 = FabricaConexoes.returnResult(stm);
                rs1.next();
                DefaultMutableTreeNode root = new DefaultMutableTreeNode(rs1.getString("nome"));
                preencheArvore(rs, root);
                MenusTreeModel model = new MenusTreeModel(root);
                jTree1.setModel(model);
                FabricaConexoes.closeConnection();
        }catch(SQLException sql)
        {
            JOptionPane.showMessageDialog(null, sql.getMessage());
        }
    }//GEN-LAST:event_jButton1ActionPerformed


    public void preencheArvore(ResultSet rs, DefaultMutableTreeNode no)
    {
                java.sql.PreparedStatement stm;

        if(rs == null)
        {

            return ;
        }

        DefaultMutableTreeNode no2, no3,no4;

        ResultSet rs1 = rs;
        ResultSet rs2 = null;
        ResultSet rs3 = null;
        ResultSet rs4 = null;
        ResultSet rs5 = null;
        ResultSet aux = null;

        try{



                while(rs1.next())
                {
                    //inicio do código que nomeia a atividade do link
                   if(FabricaConexoes.verificaConexao())
                        {
                            FabricaConexoes.getConexao();

                        }
                    stm = FabricaConexoes.returnStatement("Select nome from atividade where idatividade = ?");
                    stm.setInt(1, Integer.valueOf(String.valueOf(rs1.getObject("idat"))));
                    aux = FabricaConexoes.returnResult(stm);
                    aux.next();
                    //fim do código que nomeia a atividade do link

                    no2 = new DefaultMutableTreeNode(aux.getObject("nome"));

                    if(FabricaConexoes.verificaConexao())
                    {
                        FabricaConexoes.getConexao();
                    }
                    stm = FabricaConexoes.returnStatement("Select * from Processo_Atividade where raiz = ? and idfin != 0");
                    stm.setInt(1, Integer.valueOf(String.valueOf(rs1.getObject("chave"))));
                    rs = FabricaConexoes.returnResult(stm);

                    if(rs != null)
                    {
                        while(rs.next())
                         {

                            //inicio do bloco que nomea as finalizações
                                if(FabricaConexoes.verificaConexao())
                                    {
                                        FabricaConexoes.getConexao();

                                    }
                                stm = FabricaConexoes.returnStatement("Select nome from finalizacao where idfinalizacao = ?");
                                stm.setInt(1, Integer.valueOf(String.valueOf(rs.getObject("idfin"))));
                                aux = FabricaConexoes.returnResult(stm);
                                aux.next();
                                no3 = new DefaultMutableTreeNode(aux.getObject("nome"));
                          //fim do bloco que nomeia as finalizações

                          if(Integer.valueOf(String.valueOf(rs.getObject("link"))) != 0)
                              {

                                if(FabricaConexoes.verificaConexao())
                                        {
                                            FabricaConexoes.getConexao();

                                        }
                                stm = FabricaConexoes.returnStatement("Select * from Processo_Atividade where chave = "+rs.getObject("link"));

                                rs3 = FabricaConexoes.returnResult(stm);

                                rs3.next();
                                //inicio do código que nomeia a atividade do link
                                if(FabricaConexoes.verificaConexao())
                                    {
                                        FabricaConexoes.getConexao();

                                    }
                                stm = FabricaConexoes.returnStatement("Select nome from atividade where idatividade = ?");
                                stm.setInt(1, Integer.valueOf(String.valueOf(rs3.getObject("idat"))));
                                aux = FabricaConexoes.returnResult(stm);
                                aux.next();

                                //fim do código que nomeia a atividade do link
                                no4 = new DefaultMutableTreeNode(" [link] - " + aux.getObject("nome"));
                                no3.add(no4);

                              }
                           //código para navegar nas opções
                           if(FabricaConexoes.verificaConexao())
                                    {
                                        FabricaConexoes.getConexao();

                                    }
                          stm = FabricaConexoes.returnStatement("Select * from opcao where finalizacao = ?");
                          stm.setInt(1, Integer.valueOf(String.valueOf(rs.getObject("idfin"))));

                          rs4 = FabricaConexoes.returnResult(stm);

                          if(rs4 != null)
                          {
                              while(rs4.next())
                              {
                                  no3.add(new DefaultMutableTreeNode(String.valueOf(rs4.getString("Nome"))));

                                  //navega entre as opcoes

                                      if(FabricaConexoes.verificaConexao())
                                        {
                                            FabricaConexoes.getConexao();

                                        }
                                      stm = FabricaConexoes.returnStatement("Select * from Processo_Atividade where idop = ?");
                                      stm.setInt(1, Integer.valueOf(String.valueOf(rs4.getObject("idopcao"))));

                                      rs5 = FabricaConexoes.returnResult(stm);

                                      if(rs5 != null)
                                      {
                                          preencheArvore(rs5, no3);
                                          no2.add(no3);

                                      }

                                  //navega entre as opcoes
                              }

                          }
                          //fim código para navegar nas opções
                          /*if(FabricaConexoes.verificaConexao())
                                    {
                                        FabricaConexoes.getConexao();

                                    }
                          stm = FabricaConexoes.returnStatement("Select * from Processo_Atividade where raiz = ? and idfin = 0");
                          stm.setInt(1, Integer.valueOf(String.valueOf(rs.getObject("chave"))));

                          rs2 = FabricaConexoes.returnResult(stm);

                          if(rs2 != null)
                          {
                              preencheArvore(rs2, no3);
                              no2.add(no3);

                          }//else{no2.add(no3);}*/
                        }
                    }
                    no.add(no2);


                }

        }catch(SQLException e)
        {
            JOptionPane.showMessageDialog(null, e.getMessage());

        }

    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Teste().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree jTree1;
    // End of variables declaration//GEN-END:variables

}
